﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace PPHandler
{
    class Window
    {
        RECT rct;
        public String title { get; private set; }
        public String classname { get; private set; }
        public IntPtr handle { get; private set; }
        public LinkedList<Window> lstWindows = new LinkedList<Window>();

        public Window(IntPtr handle)
        {
            this.handle = handle;
            StringBuilder sb = new StringBuilder(1024);
            StringBuilder sbc = new StringBuilder(256);

            //const int WM_GETTEXT = 0xD;
            // needs to be big enough for the whole text
            //User32.SendMessage(handle, WM_GETTEXT, sb.Length, sb);
            
            //Console.WriteLine("-" + sb.ToString());

            User32.GetClassName((int)handle, sbc, sbc.Capacity);
            User32.GetWindowText((int)handle, sb, sb.Capacity);

            this.title = sb.ToString();
            this.classname = sbc.ToString();

            this.rct = new RECT(); //Fensterposition und Größe bestimmen:
            User32.GetWindowRect(handle, ref this.rct);
        }

        public int getUpperLeftX()
        {
            return rct.Left;
        }

        public int getUpperLeftY()
        {
            return rct.Top;
        }

        public void show()
        {
            User32.ShowWindow(handle, ShowWindowCommands.Restore);
            User32.SetForegroundWindow(handle);
            User32.SetActiveWindow(handle);
        }

        ///
        /// Delegate Funktion für EnumWindows (Siehe Declarations)
        /// Gibt die Werte an EnumWindowCallBack weiter
        ///
        public delegate bool WinCallBack(int hwnd, int lParam);

        public void getChildWindows()
        {
            //Liste mit Fenstern befüllen
            User32.EnumChildWindows(handle, new WinCallBack(EnumChildWindowCallBack), 0);
        }

        ///
        /// Diese Funktion wird durch die Delegate Funktion WinCallBack aufgerufen
        /// und iteriert durch alle zur Zeit geöffneten Fenster
        private bool EnumChildWindowCallBack(int hwnd, int lParam)
        {
            this.handle = handle;
            StringBuilder sb = new StringBuilder(1024);

            const int WM_GETTEXT = 0xD;
            // needs to be big enough for the whole text
            User32.SendMessage(handle, WM_GETTEXT, sb.Length, sb);

            Console.WriteLine("-" + sb.ToString());

            return true;
        }

        ///
        /// Delegate Funktion für EnumPropss (Siehe Declarations)
        /// Gibt die Werte an EnumWindowPropsCallBack weiter
        ///
        public delegate bool EnumPropsDelegate(int hwnd, string prop, IntPtr data);

        public void getWindowProps()
        {
            User32.EnumProps(handle, new EnumPropsDelegate(WindowEnumPropsCallBack));
        }

        ///
        /// Diese Funktion wird durch die Delegate Funktion EnumCallBack aufgerufen
        /// und iteriert durch alle zur Zeit geöffneten Fenster
        private bool WindowEnumPropsCallBack(int hwnd, string prop, IntPtr data)
        {
            StringBuilder sb = new StringBuilder(1024);

            const int WM_GETTEXT = 0xD;
            // needs to be big enough for the whole text
            User32.SendMessage(handle, WM_GETTEXT, sb.Length, sb);

            Console.WriteLine("Prop:" + prop);

            return true;
        }
    }

}
